package com.yanqu.road.dao.impl.activity.turtle;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.turtle.data.TurtleNotice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-09-13
 * @Description : 乌龟对对碰-Notice Dao
 */

public class TurtleNoticeDaoImpl extends TempDao {
    public static String getTurtleNoticeDataSql = "select * from `t_c_turtle_notice` where `activityId`=? order by `noticeTime` desc limit 100";

    public List<TurtleNotice> getTurtleNoticeData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<TurtleNotice> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getTurtleNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseData(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertTurtleDrawNoticeDataSql = "insert into `t_c_turtle_notice`(activityId,userId,serverId,nickName,modelType,reward,noticeTime) values (?,?,?,?,?,?,?)";

    public boolean addNoticeList(List<TurtleNotice> noticeDataList) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<Map<Integer, DbParameter>>();
        for (TurtleNotice noticeData : noticeDataList) {
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            int i = 1;

            para.put(i++, new DbParameter(Types.INTEGER, noticeData.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, noticeData.getNickName()));
            para.put(i++, new DbParameter(Types.INTEGER, noticeData.getModelType()));
            para.put(i++, new DbParameter(Types.VARCHAR, noticeData.getReward()));
            para.put(i++, new DbParameter(Types.BIGINT, noticeData.getNoticeTime()));

            mapList.add(para);
        }

        if (getDb().executeBatchQuery(insertTurtleDrawNoticeDataSql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    protected TurtleNotice parseData(ResultSet rs) throws SQLException {
        TurtleNotice noticeData = new TurtleNotice();
        noticeData.setActivityId(rs.getInt("activityId"));
        noticeData.setUserId(rs.getLong("userId"));
        noticeData.setServerId(rs.getLong("serverId"));
        noticeData.setNickName(rs.getString("nickName"));
        noticeData.setModelType(rs.getInt("modelType"));
        noticeData.setReward(rs.getString("reward"));
        noticeData.setNoticeTime(rs.getLong("noticeTime"));

        return noticeData;
    }
}
